#include "Bill.h"
#include "Hotel.h"

/*
==================
Bill::Bill
==================
*/
Bill::Bill(ifstream &file) {
	getline(file, description);

	file >> sum;
	file.ignore();
}

/*
==================
Bill::Bill

Read input from the command line.
==================
*/
Bill::Bill(bool readData) {
	if (readData) {
		readInput();
	}
}

/*
==================
Bill::WriteToFile

Write relevant data to file.
==================
*/
void Bill::WriteToFile(ofstream &file) {
	file << description;
	file <<endl;
	
	file << sum;
	file <<endl;
}

/*
==================
Bill::setData
==================
*/
void Bill::setData(string d, float s) {
	description = d;
	sum = s;
}

/*
==================
Bill::GetDescription
==================
*/
string Bill::GetDescription() {
	return description;
}

/*
==================
Bill::GetSum
==================
*/
float Bill::GetSum() {
	return sum;
}

/*
==================
Bill::display
==================
*/
void Bill::display() {
	printf("\t\t%s\t\tPrice: %0.2f\n",description.c_str(), sum);
}

/*
==================
Bill::readInput

Either an integral value (a pre-defined item) or a string (new, custom item)
is allowed as the description.
==================
*/
void Bill::readInput() {	
	unsigned max = Singletons::billRecord->getCount();
	
	/* Print all recorded billing records */
	printf("\tBilling records:\n");
	for (unsigned i = 0; i < max; i++) {
		printf("\t\t[%i]\t%s\n", i+1, Singletons::billRecord->getRecord(i).c_str());
	}
	printf("\n");
	
	/* Input loop: Accept either an integer or a string */
	string selection;
	while (!selection.length()) {
		string input = ReadString("Selection");
		
		stringstream ss(input);
		int numSel;
		if (ss >> numSel) {
			if (numSel >= 1 && numSel <= max) {
				selection = Singletons::billRecord->getRecord(numSel-1);
			} else {
				printf("\tIn the range 1 to %i, please.\n", max);
			}
		} else {
			if (input.length() > 2) {
				selection = input;
			}
		}
	}
	
	description = selection;

	/* If breakfast is selected, use the standard price
	 * for breakfast in hotel. */
	if (capitalize(description) == "BREAKFAST") {
		sum = Singletons::hotel->getBreakfastPrice();
		printf("\tPrice of breakfast: %0.2f\n", sum);
	} else {
		/* Read sum */
		sum = ReadFloat("Sum", 25.f);
	}
}























